package com.amigo.online.provider.manager.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.provider.manager.model.entity.User;
import com.amigo.online.provider.manager.mongo.dao.UserDao;
import com.amigo.online.provider.manager.mongo.entity.UserEntity;
import com.amigo.online.provider.manager.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 用户相关
 *
 * @author amigo
 */
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @Autowired
    private UserDao userDao;

    /**
     * 查询用户,条件查询
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/getUserList")
    public ResponseEntity<Response> getUserList(@RequestBody User user) {
        JSONObject result = new JSONObject();
        // 分页语句
        Page<Object> page = PageHelper.startPage(user.getPageNo(), 10);
        List<User> list = userService.getUserList(user);
        result.put("userlist", list);
        // 总记录数
        result.put("pagetotal", page.getTotal());
        return success(result);
    }

    /**
     * 查询mongodb 查询今天登陆者的 手机号 头像 设备登陆时间等信息
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/getUserEntityList")
    public String getUserEntityList(@RequestBody UserEntity user) {
        JSONObject result = new JSONObject();

        List<UserEntity> list = userDao.getLoginTimeUser(user);
        int loginTimeUserSize = userDao.getLoginTimeUserSize(user);
        result.put("userEntityList", list);
        result.put("pagetotal", loginTimeUserSize);
        return result.toJSONString();
    }

    /**
     * 添加用户
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/insertionUser")
    public ResponseEntity<Response> insertionUser(@RequestBody User user) {

        return success(userService.insertionUser(user));

    }

    /**
     * 修改用户
     *
     * @param data
     * @return
     */
    @PutMapping(value = "/updateUser")
    public ResponseEntity<Response> updateUser(@RequestBody User user) {

        return success(userService.updateUser(user));

    }

}
